<?php

namespace App\Http\Controllers\Api;


use App\Models\Wxcard;
use App\Models\Wxuser;
use App\Models\WxuserWxcard;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class CardController extends Controller
{
    use Helpers;

    const ERR_CODE = array(
        'SUCCESS' => '100',
        'CARD_WXUSER_NOTFOUND' => '1101',
        'CARD_NOTFOUND' => '1102',
    );

    public function add(Request $request)
    {
        $id_wxuser = $request->post('wxuser_id', null);
        $card_id = $request->post('card_id', null);
        $code = $request->post('code', 'none');

        $wxuser = Wxuser::where('id', $id_wxuser)->first();

        if(empty($wxuser)){
            return $this->response->array(array(
                'message' => 'id_wxuser invalid.',
                'code' => self::ERR_CODE['CARD_WXUSER_NOTFOUND']
            ));
        }
        $wxcard = Wxcard::where('card_id', $card_id)->first();
        if(empty($wxcard)){
            return $this->response->array(array(
                'message' => 'card_id invalid.',
                'code' => self::ERR_CODE['CARD_NOTFOUND']
            ));
        }

       $res = WxuserWxcard::create([
           'id_wxusers'=>$wxuser->id,
           'id_wxcards'=>$wxcard->id,
           'code' => $code
       ]);
        return $this->response->array(array(
            'message' => 'success',
            'code' => self::ERR_CODE['SUCCESS']
        ));
    }
}
